package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.product.domain.InsProjectCodeNo;
import com.ruoyi.project.product.service.IInsProjectCodeNoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 产品代码Controller
 *
 * @author taney
 * @date 2020-12-11
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/projectCodeNo")
public class InsProjectCodeNoController extends BaseController {

    private final IInsProjectCodeNoService iInsProjectCodeNoService;

    /**
     * 查询产品代码列表
     */
    @PreAuthorize("@ss.hasPermi('product:projectCodeNo:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsProjectCodeNo insProjectCodeNo) {
        startPage();
        LambdaQueryWrapper<InsProjectCodeNo> lqw = new LambdaQueryWrapper<InsProjectCodeNo>();
        if (insProjectCodeNo.getProjectId() != null) {
            lqw.eq(InsProjectCodeNo::getProjectId, insProjectCodeNo.getProjectId());
        }
        if (StringUtils.isNotBlank(insProjectCodeNo.getMachineType())) {
            lqw.eq(InsProjectCodeNo::getMachineType, insProjectCodeNo.getMachineType());
        }
        if (StringUtils.isNotBlank(insProjectCodeNo.getCodeNo())) {
            lqw.eq(InsProjectCodeNo::getCodeNo, insProjectCodeNo.getCodeNo());
        }
        if (insProjectCodeNo.getLotBarcode() != null) {
            lqw.eq(InsProjectCodeNo::getLotBarcode, insProjectCodeNo.getLotBarcode());
        }
        if (StringUtils.isNotBlank(insProjectCodeNo.getOpenAccessCode())) {
            lqw.eq(InsProjectCodeNo::getOpenAccessCode, insProjectCodeNo.getOpenAccessCode());
        }
        if (StringUtils.isNotBlank(insProjectCodeNo.getCloseAccessCode())) {
            lqw.eq(InsProjectCodeNo::getCloseAccessCode, insProjectCodeNo.getCloseAccessCode());
        }
        List<InsProjectCodeNo> list = iInsProjectCodeNoService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出产品代码列表
     */
    @PreAuthorize("@ss.hasPermi('product:projectCodeNo:export')")
    @Log(title = "产品代码", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsProjectCodeNo insProjectCodeNo) {
        LambdaQueryWrapper<InsProjectCodeNo> lqw = new LambdaQueryWrapper<InsProjectCodeNo>(insProjectCodeNo);
        List<InsProjectCodeNo> list = iInsProjectCodeNoService.list(lqw);
        ExcelUtil<InsProjectCodeNo> util = new ExcelUtil<InsProjectCodeNo>(InsProjectCodeNo.class);
        return util.exportExcel(list, "projectCodeNo");
    }

    /**
     * 获取产品代码详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:projectCodeNo:query')")
    @GetMapping(value = "/{projectCodeNoId}")
    public AjaxResult getInfo(@PathVariable("projectCodeNoId") Integer projectCodeNoId) {
        return AjaxResult.success(iInsProjectCodeNoService.getById(projectCodeNoId));
    }

    /**
     * 新增产品代码
     */
    @PreAuthorize("@ss.hasPermi('product:projectCodeNo:add')")
    @Log(title = "产品代码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsProjectCodeNo insProjectCodeNo) {
        return toAjax(iInsProjectCodeNoService.save(insProjectCodeNo) ? 1 : 0);
    }

    /**
     * 修改产品代码
     */
    @PreAuthorize("@ss.hasPermi('product:projectCodeNo:edit')")
    @Log(title = "产品代码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsProjectCodeNo insProjectCodeNo) {
        return toAjax(iInsProjectCodeNoService.updateById(insProjectCodeNo) ? 1 : 0);
    }

    /**
     * 删除产品代码
     */
    @PreAuthorize("@ss.hasPermi('product:projectCodeNo:remove')")
    @Log(title = "产品代码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectCodeNoIds}")
    public AjaxResult remove(@PathVariable Integer[] projectCodeNoIds) {
        return toAjax(iInsProjectCodeNoService.removeByIds(Arrays.asList(projectCodeNoIds)) ? 1 : 0);
    }

    /**
     * 获取产品代码详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:projectCodeNo:getByMachineTypeAndProjectId')")
    @GetMapping(value = "/getByMachineTypeAndProjectId")
    public AjaxResult getByMachineTypeAndProjectId(InsProjectCodeNo insProjectCodeNo) {
        LambdaQueryWrapper<InsProjectCodeNo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InsProjectCodeNo::getMachineType, insProjectCodeNo.getMachineType());
        queryWrapper.eq(InsProjectCodeNo::getProjectId, insProjectCodeNo.getProjectId());
        return AjaxResult.success(iInsProjectCodeNoService.getOne(queryWrapper));
    }
}
